using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace IFXplus.SmartStorage.Settings
{
    public class FileSystemStorageProviderSettings : BaseStorageProviderSettings
    {

        #region StorageProviderType

        public override StorageProviderType Type
        {
            get { return StorageProviderType.FileSystem; }
        }

        #endregion

        [ConfigurationProperty("path", DefaultValue = @"~\App_Data\", IsKey = false, IsRequired = true)]
        [StringValidator(InvalidCharacters = " !@#$%^&*()[]{}/;'\"|", MinLength = 3, MaxLength = 100)]
        public string Path
        {
            get
            {
                string path = (string) base["path"];
                if (path.StartsWith(@"~\"))
                    path = path.Replace(@"~\", AppDomain.CurrentDomain.BaseDirectory);
                return path;
            }
            set { base["path"] = value; }
        }

        [ConfigurationProperty("maxStorageSize", DefaultValue = "0", IsKey = false, IsRequired = true)]
        public string MaxStorageSize
        {
            get { return (string) base["maxStorageSize"]; }
            set { base["maxStorageSize"] = value; }
        }

        [ConfigurationProperty("debugMode", DefaultValue = "false", IsKey = false, IsRequired = true)]
        public bool DebugMode
        {
            get { return (bool)base["debugMode"]; }
            set { base["debugMode"] = value; }
        }

        [ConfigurationProperty("useScavenging", DefaultValue = "true", IsKey = false, IsRequired = true)]
        public bool UseScavenging
        {
            get { return (bool)base["useScavenging"]; }
            set { base["maxStorageSize"] = value; }
        }

        [ConfigurationProperty("minFreeSpaceAfterScavenging", DefaultValue = "0", IsKey = false, IsRequired = true)]
        public string MinFreeSpaceAfterScavenging
        {
            get { return (string)base["minFreeSpaceAfterScavenging"]; }
            set { base["minFreeSpaceAfterScavenging"] = value; }
        }

        [ConfigurationProperty("scavengingInterval", DefaultValue = "1.00:00:00.000", IsKey = false, IsRequired = true)]
        public TimeSpan ScavengingInterval
        {
            get { return (TimeSpan)base["scavengingInterval"]; }
            set { base["scavengingInterval"] = value; }
        }

        [ConfigurationProperty("scavengingLogFile", DefaultValue = @"~\App_Data\logs.txt", IsKey = false, IsRequired = true)]
        [StringValidator(InvalidCharacters = " !@#$%^&*()[]{}/;'\"|", MinLength = 3, MaxLength = 100)]
        public string ScavengingLogFile
        {
            get
            {
                string path = (string)base["scavengingLogFile"];
                if (path.StartsWith(@"~\"))
                    path = path.Replace(@"~\", AppDomain.CurrentDomain.BaseDirectory);
                return path;
            }
            set { base["scavengingLogFile"] = value; }
        }

        public long MaxStorageSizeBytes
        {
            get { return GetBytes(MaxStorageSize); }
        }

        public long MinFreeSpaceAfterScavengingBytes
        {
            get { return GetBytes(MinFreeSpaceAfterScavenging); }
        }

        private static long GetBytes(string sizeDescription)
        {
            string size = sizeDescription.Trim().ToUpper();

            long unit = 1;                  // (default) = 1024^0 = 1
            if (size.EndsWith("KB"))
                unit = 1024;                // KB = 1024^1 = 1024
            else if (size.EndsWith("MB"))
                unit = 1048576;             // MB = 1024^2 = 1048576
            else if (size.EndsWith("GB"))
                unit = 1073741824;          // GB = 1024^3 = 1073741824

            size = (new Regex(@"([^0-9])")).Replace(size, "");

            long bytes;
            long.TryParse(size, out bytes);
            return (bytes * unit);
        }

    }
}
